//
//  DesignerDetailViewController.m
//  ShrimpCrowdordering
//
//  Created by 郑洲 on 2018/7/26.
//  Copyright © 2018年 郑洲. All rights reserved.
//

#import "DesignerDetailViewController.h"

#import "MoreDetailViewController.h"

#import "ChannelDetailCell.h"

#import "ShareView.h"
#import "UIImage+GradientColor.h"

#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import "WeiboSDK.h"
#import "WXApi.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>

@interface DesignerDetailViewController ()<UITableViewDelegate, UITableViewDataSource, TencentSessionDelegate, UITextViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSDictionary *personDict;

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger totalPage;

@property (nonatomic, strong) UIButton *shadowBtn;
@property (nonatomic, strong) ShareView *shareView;
@property (nonatomic, copy) NSString *proId;
@property (nonatomic, copy) NSString *shareId;
@property (nonatomic, copy) NSString *shareName;
@property (nonatomic, copy) NSString *shareLink;

@property (nonatomic, strong) TencentOAuth *tencentOAuth;

@property (nonatomic, strong) UIView *commentView;
@property (nonatomic, strong) NSString *appraiseId;

@property (nonatomic, strong) AVPlayer *player;
@property (nonatomic, strong) AVPlayerViewController  *playerView;

@end

@implementation DesignerDetailViewController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [kNotificationCenter addObserver:self selector:@selector(shareSuccess) name:@"WechatShare" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    [self.shadowBtn removeFromSuperview];
    [self.shareView removeFromSuperview];
    [kNotificationCenter removeObserver:self name:@"WechatShare" object:nil];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat yoffset = scrollView.contentOffset.y;
    
    if (yoffset >0) {
        
        CGFloat alpha = MIN(1, yoffset/(ScreenX375(355) + STATUS_BAR_HEIGHT)); //150：上拉150距离背景图片alpha=1
        if (alpha > 0.99) {
            [self.navigationController.navigationBar setShadowImage:[HttpManager createImageWithColor:REXADECIMALCOLOR(@"#f4f4f4")]];
        }else {
            [self.navigationController.navigationBar setShadowImage:[UIImage new]];
        }
        
        UIImage *image = [HttpManager createImageWithColor:RGBACOLOR(255, 255, 255, alpha)];
        
        [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = [NSMutableArray array];
    self.currentPage = 1;
    self.proId = @"";
    
    self.tableView = [[BaseTableView alloc] initWithFrame:CGRectMake(0, -NAVIGATION_BAR_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = ScreenX375(340);
    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.tableView];
    
    NSMutableArray *arr = [NSMutableArray array];
    for (int i = 0; i < 12; i++) {
        NSString *imageName = [NSString stringWithFormat:@"%d",i];
        [arr addObject:ImageWithName(imageName)];
    }
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    [header setImages:arr forState:MJRefreshStateRefreshing];
    self.tableView.mj_header = header;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    
    [self getHeadData];
}

- (void)getHeadData {
    NSDictionary *parameter = @{@"userId":kNSUDefaultReadKey(@"userId"),@"id":_designerId};
    [HttpManager postHttpRequestByGet:[NSString stringWithFormat:@"%@api/getUserInfo",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            self.personDict = successResponse[@"data"];
            [self setHead];
            [self getData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)shareSuccess {
    NSDictionary *parameter = @{@"id":_shareId,@"userId":kNSUDefaultReadKey(@"userId")};
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/shareSuccess",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            [HttpManager showNoteMsg:@"分享成功"];
            self.proId = @"";
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)getShare {
    NSDictionary *parameter = @{@"proId":_designerId,@"userId":kNSUDefaultReadKey(@"userId"),@"type":@"0"};
    if (_proId.length > 0) {
        parameter = @{@"proId":_proId,@"userId":kNSUDefaultReadKey(@"userId"),@"type":@"3"};
    }else {
        self.shareName = _personDict[@"userInfo"][@"nickName"];
    }
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/share",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            self.shareId = [NSString stringWithFormat:@"%@",successResponse[@"data"][@"id"]];
            self.shareLink = [NSString stringWithFormat:@"%@",successResponse[@"data"][@"url"]];
            [self share];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)headerRefresh {
    _currentPage = 1;
    [self getData];
    self.tableView.mj_footer.hidden = NO;
}

- (void)footerRefresh {
    if (_currentPage <= _totalPage) {
        
        _currentPage++;
        [self getData];
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
    }else{
        
//        [HttpManager showNoteMsg:@"已经到底啦"];
        // 结束刷新
        [self.tableView.mj_footer endRefreshing];
        self.tableView.mj_footer.hidden = YES;
    }
}

- (void)getData {
    NSMutableDictionary *parameter = [NSMutableDictionary dictionaryWithDictionary:@{@"userId":_personDict[@"userInfo"][@"id"],@"pageSize":@"10",@"page":[NSNumber numberWithInteger:_currentPage]}];
    [HttpManager postHttpRequestByGet:[NSString stringWithFormat:@"%@api/otherProList",REQUESTHEADER] andParameter:parameter success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            if (self.currentPage == 1) {
                [self.dataArray removeAllObjects];
            }
            self.totalPage = [successResponse[@"data"][@"pages"] integerValue];
            NSError *error = nil;
            for (NSDictionary *dic in successResponse[@"data"][@"list"]) {
                ChannelListModel *model = [[ChannelListModel alloc] initWithDictionary:dic error:&error];
                [self.dataArray addObject:model];
            }
            if (self.totalPage == self.currentPage) {
                self.tableView.mj_footer.hidden = YES;
            }
            if ([successResponse[@"data"][@"list"] count] == 0) {
                [HttpManager showNoteMsg:@"暂无设计作品"];
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
        if ([self.tableView.mj_header isRefreshing]) {
            [self.tableView.mj_header endRefreshing];
        }else if ([self.tableView.mj_footer isRefreshing]) {
            [self.tableView.mj_footer endRefreshing];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}


- (void)setHead {
    UIButton *shareBtn = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - ScreenX375(44), STATUS_BAR_HEIGHT, ScreenX375(44), ScreenX375(44))];
    [shareBtn setImage:ImageWithName(@"分享") forState:UIControlStateNormal];
    shareBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 5, 0, -5);
    [shareBtn addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shareBtn];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(322) + STATUS_BAR_HEIGHT)];
    self.tableView.tableHeaderView = headView;
    
    UIImageView *bgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ScreenX375(193) + STATUS_BAR_HEIGHT)];
    bgView.image = ImageWithName(@"背景图");
    [headView addSubview:bgView];
    
    UIView *allBg = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(18), ScreenX375(147) + STATUS_BAR_HEIGHT, (SCREEN_WIDTH - ScreenX375(36)), ScreenX375(175))];
    allBg.backgroundColor = [UIColor whiteColor];
    allBg.layer.cornerRadius = ScreenX375(8);
    allBg.layer.shadowColor = RGBACOLOR(0, 0, 0, 0.06).CGColor;
    allBg.layer.shadowOpacity = 1;
    allBg.layer.shadowOffset = CGSizeMake(0, 3);
    allBg.layer.shadowRadius = 15;
    [headView addSubview:allBg];
    
    UIImageView *headImage = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(96)) / 2, ScreenX375(99) + STATUS_BAR_HEIGHT, ScreenX375(96), ScreenX375(96))];
    headImage.layer.cornerRadius = ScreenX375(57);
    headImage.image = [UIImage imageNamed:@"头像1"];
    headImage.layer.cornerRadius = ScreenX375(48);
    headImage.clipsToBounds = YES;
    [headImage sd_setImageWithURL:[NSURL URLWithString:_personDict[@"userInfo"][@"pic"]] placeholderImage:nil options:SDWebImageRetryFailed];
    headImage.layer.borderWidth = ScreenX375(4);
    headImage.layer.borderColor = REXADECIMALCOLOR(@"#ffffff").CGColor;
    [headView addSubview:headImage];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenX375(60), SCREEN_WIDTH - ScreenX375(36), ScreenX375(24))];
    nameLabel.textColor = REXADECIMALCOLOR(@"#222222");
    nameLabel.textAlignment = NSTextAlignmentCenter;
    nameLabel.font = KBlodfont(16);
    nameLabel.text = [NSString stringWithFormat:@"%@",_personDict[@"userInfo"][@"nickName"]];
    [allBg addSubview:nameLabel];
    
    UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenX375(84), SCREEN_WIDTH - ScreenX375(36), ScreenX375(17))];
    infoLabel.textColor = REXADECIMALCOLOR(@"#9b9b9b");
    infoLabel.font = Kfont(12);
    infoLabel.text = [NSString stringWithFormat:@"%@",_personDict[@"userInfo"][@"intro"]];
    infoLabel.textAlignment = NSTextAlignmentCenter;
    [allBg addSubview:infoLabel];
    
    UILabel *fenNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, ScreenX375(110), (SCREEN_WIDTH - ScreenX375(36)) / 2, ScreenX375(17))];
    fenNumLabel.textColor = REXADECIMALCOLOR(@"#f21934");
    fenNumLabel.font = Kfont(13);
    fenNumLabel.textAlignment = NSTextAlignmentRight;
    [allBg addSubview:fenNumLabel];
    
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"粉丝数:%@",_personDict[@"fensNum"]]];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:REXADECIMALCOLOR(@"#000000") range:NSMakeRange(0, 4)];
    fenNumLabel.attributedText = attributedStr;
    
    UILabel *noLabel = [[UILabel alloc] initWithFrame:CGRectMake(ScreenX375(203), ScreenX375(110), ScreenX375(100), ScreenX375(17))];
    noLabel.textColor = REXADECIMALCOLOR(@"#f5a623");
    noLabel.font = Kfont(13);
    [allBg addSubview:noLabel];
    
    NSMutableAttributedString *attributedStr1 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"排名:%@",_personDict[@"no"]]];
    [attributedStr1 addAttribute:NSForegroundColorAttributeName value:REXADECIMALCOLOR(@"#000000") range:NSMakeRange(0, 3)];
    noLabel.attributedText = attributedStr1;
    
    UIButton *concernBtn = [[UIButton alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - ScreenX375(100)) / 2, ScreenX375(137), ScreenX375(64), ScreenX375(26))];
    if ([_personDict[@"isFollow"] integerValue]) {
        [concernBtn setTitle:@"已关注" forState:UIControlStateNormal];
        [concernBtn setTitleColor:REXADECIMALCOLOR(@"#212121") forState:UIControlStateNormal];
        [concernBtn setImage:nil forState:UIControlStateNormal];
    }else {
        [concernBtn setTitle:@" 关注" forState:UIControlStateNormal];
        [concernBtn setTitleColor:REXADECIMALCOLOR(@"#ff2469") forState:UIControlStateNormal];
        [concernBtn setImage:ImageWithName(@"添加关注") forState:UIControlStateNormal];
    }
    concernBtn.titleLabel.font = Kfont(14);
    concernBtn.layer.cornerRadius = ScreenX375(13);
    concernBtn.layer.borderWidth = 1;
    concernBtn.layer.borderColor = REXADECIMALCOLOR(@"#eaeaea").CGColor;
    [concernBtn addTarget:self action:@selector(concern:) forControlEvents:UIControlEventTouchUpInside];
    [allBg addSubview:concernBtn];
}

- (void)share {
    if (!self.shareView) {
        self.shadowBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.shadowBtn.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        [self.shadowBtn addTarget:self action:@selector(hidden) forControlEvents:UIControlEventTouchUpInside];
        self.shadowBtn.hidden = YES;
        [kKeyWindow addSubview:self.shadowBtn];
        
        self.shareView = [[ShareView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT , SCREEN_WIDTH, ScreenX375(180))];
        __weak __typeof(self) weakSelf = self;
        self.shareView.cancel = ^{
            [weakSelf hidden];
        };
        self.shareView.wxClick = ^{
            SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
            sendReq.bText = NO;
            sendReq.scene = 0;
            
            // 2.创建分享内容
            WXMediaMessage *message = [WXMediaMessage message];
            //分享标题
            message.title = @"虾拼";
            // 描述
            message.description = weakSelf.shareName;
            //分享图片,使用SDK的setThumbImage方法可压缩图片大小
            //                message.thumbData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGEHEADER,self.model.pics.firstObject]]];
            [message setThumbImage:[UIImage imageNamed:@"虾拼 logo"]];
            
            //创建多媒体对象
            WXWebpageObject *webObj = [WXWebpageObject object];
            // 点击后的跳转链接
            webObj.webpageUrl = weakSelf.shareLink;
            message.mediaObject = webObj;
            sendReq.message = message;
            [WXApi sendReq:sendReq];
            [weakSelf hidden];
        };
        self.shareView.wxGroupClick = ^{
            SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc] init];
            sendReq.bText = NO;
            sendReq.scene = 1;
            
            // 2.创建分享内容
            WXMediaMessage *message = [WXMediaMessage message];
            //分享标题
            message.title = @"虾拼";
            // 描述
            message.description = weakSelf.shareName;
            //分享图片,使用SDK的setThumbImage方法可压缩图片大小
            [message setThumbImage:[UIImage imageNamed:@"虾拼 logo"]];
            
            //创建多媒体对象
            WXWebpageObject *webObj = [WXWebpageObject object];
            // 点击后的跳转链接
            webObj.webpageUrl = weakSelf.shareLink;
            message.mediaObject = webObj;
            sendReq.message = message;
            [WXApi sendReq:sendReq];
            [weakSelf hidden];
        };
        self.shareView.qqClick = ^{
            weakSelf.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQAppKey andDelegate:weakSelf];
            
            NSString *url = weakSelf.shareLink;
            NSString *imageUrl = [NSString stringWithFormat:@"%@",IMAGEHEADER];
            
            QQApiNewsObject *share = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:@"虾拼" description:weakSelf.shareName previewImageURL:[NSURL URLWithString:imageUrl]];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:share];
            
            //分享到qq
            [QQApiInterface sendReq:req];
            [weakSelf hidden];
        };
        self.shareView.qqGroupClick = ^{
            weakSelf.tencentOAuth = [[TencentOAuth alloc] initWithAppId:kQQAppKey andDelegate:weakSelf];
            
            NSString *url = weakSelf.shareLink;
            NSString *imageUrl = [NSString stringWithFormat:@"%@",IMAGEHEADER];
            
            QQApiNewsObject *share = [QQApiNewsObject objectWithURL:[NSURL URLWithString:url] title:@"虾拼" description:weakSelf.shareName previewImageURL:[NSURL URLWithString:imageUrl]];
            SendMessageToQQReq *req = [SendMessageToQQReq reqWithContent:share];
            
            //分享到qq
            [QQApiInterface SendReqToQZone:req];
            [weakSelf hidden];
        };
        self.shareView.wbClick = ^{
            //微博
            WBMessageObject *message = [WBMessageObject message];
            message.text = [NSString stringWithFormat:@"虾拼 %@  %@",weakSelf.shareName,weakSelf.shareLink];
            
            WBAuthorizeRequest *authRequest = [WBAuthorizeRequest request];
            authRequest.redirectURI = kWeiboAppRedirectURL;
            authRequest.scope = @"all";
            
            WBSendMessageToWeiboRequest *sharerequest = [WBSendMessageToWeiboRequest requestWithMessage:message authInfo:authRequest access_token:nil];
            [WeiboSDK sendRequest:sharerequest];
            [weakSelf hidden];
        };
        [kKeyWindow addSubview:self.shareView];
    }
    self.shadowBtn.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.shareView.frame = CGRectMake(0, SCREEN_HEIGHT - ScreenX375(180), SCREEN_WIDTH, ScreenX375(180));
    }];
}

- (void)hidden {
    [UIView animateWithDuration:0.3 animations:^{
        self.shareView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, ScreenX375(180));
    } completion:^(BOOL finished) {
        self.shadowBtn.hidden = YES;
    }];
}

- (void)concern:(UIButton *)concernBtn {
    if ([concernBtn.titleLabel.text isEqualToString:@"已关注"]) {
        [HttpManager showNoteMsg:@"取消关注成功"];
    }else {
        [HttpManager showNoteMsg:@"关注成功"];
    }
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/concern",REQUESTHEADER] andParameter:@{@"concernId":kNSUDefaultReadKey(@"userId"),@"concernedId":_designerId} success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            if ([concernBtn.titleLabel.text isEqualToString:@" 关注"]) {
                [concernBtn setTitle:@"已关注" forState:UIControlStateNormal];
                [concernBtn setTitleColor:REXADECIMALCOLOR(@"#212121") forState:UIControlStateNormal];
                [concernBtn setImage:nil forState:UIControlStateNormal];
            }else {
                [concernBtn setTitle:@" 关注" forState:UIControlStateNormal];
                [concernBtn setTitleColor:REXADECIMALCOLOR(@"#ff2469") forState:UIControlStateNormal];
                [concernBtn setImage:ImageWithName(@"添加关注") forState:UIControlStateNormal];
            }
        }else {
            [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

- (void)backClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChannelDetailCell *cell = [ChannelDetailCell cellWithTableView:tableView andIndexPath:indexPath];
    cell.channelListModel = _dataArray[indexPath.row];
    ChannelListModel *model = _dataArray[indexPath.row];
    cell.appraiseClick = ^{
        [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/zan",REQUESTHEADER] andParameter:@{@"proId":model.Id,@"userId":kNSUDefaultReadKey(@"userId"),@"type":@"0"} success:^(id successResponse) {
            NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
            if ([status isEqualToString:@"200"]) {
                if ([model.isZan integerValue]) {
                    model.isZan = @"0";
                    model.zanNum = [NSString stringWithFormat:@"%ld",[model.zanNum integerValue] - 1];
                }else {
                    model.isZan = @"1";
                    model.zanNum = [NSString stringWithFormat:@"%ld",[model.zanNum integerValue] + 1];
                }
                [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            }else {
                [HttpManager showNoteMsg:[NSString stringWithFormat:@"%@",successResponse[@"msg"]]];
            }
        } andFailure:^(id failureResponse) {
            [HttpManager showFail];
        }];
    };
    cell.commentClick = ^{
        self.appraiseId = model.Id;
        [self showComment];
    };
    cell.shareClick = ^{
        self.proId = model.Id;
        self.shareName = model.proName;
        [self getShare];
    };
    cell.playClick = ^{
        [self playVideoWithUrl:model.videoUrl];
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ChannelListModel *model = _dataArray[indexPath.row];
    MoreDetailViewController *vc = [[MoreDetailViewController alloc] init];
    vc.proId = model.Id;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)playVideoWithUrl:(NSString *)url {
    NSString *playString = url;
    //视频播放的url
    NSURL *playerURL = [NSURL URLWithString:playString];
    //初始化
    self.playerView = [[AVPlayerViewController alloc]init];
    //AVPlayerItem 视频的一些信息  创建AVPlayer使用的
    AVPlayerItem *item = [[AVPlayerItem alloc]initWithURL:playerURL];
    //通过AVPlayerItem创建AVPlayer
    self.player = [[AVPlayer alloc]initWithPlayerItem:item];
    //给AVPlayer一个播放的layer层
    AVPlayerLayer *layer = [AVPlayerLayer playerLayerWithPlayer:self.player];
    layer.frame = CGRectMake(0, 100, self.view.frame.size.width, 200);
    layer.backgroundColor = [UIColor greenColor].CGColor;
    //设置AVPlayer的填充模式
    layer.videoGravity = AVLayerVideoGravityResize;
    //设置AVPlayerViewController内部的AVPlayer为刚创建的AVPlayer
    self.playerView.player = self.player;
    //关闭AVPlayerViewController内部的约束
    self.playerView.view.translatesAutoresizingMaskIntoConstraints = YES;
    [self presentViewController:self.playerView animated:YES completion:nil];
}


- (void)close {
    [_commentView removeFromSuperview];
}

- (void)showComment {
    if (!_commentView) {
        _commentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _commentView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        
        UIView *commentBg = [[UIView alloc] initWithFrame:CGRectMake(ScreenX375(24), ScreenX375(210), SCREEN_WIDTH - ScreenX375(48), ScreenX375(194))];
        commentBg.backgroundColor = REXADECIMALCOLOR(@"#f5f5f5");
        commentBg.layer.cornerRadius = ScreenX375(8);
        commentBg.clipsToBounds = YES;
        [_commentView addSubview:commentBg];
        
        UIButton *closeBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, ScreenX375(29), ScreenX375(29))];
        [closeBtn setImage:ImageWithName(@"关闭") forState:UIControlStateNormal];
        [closeBtn addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
        [commentBg addSubview:closeBtn];
        
        UITextView *commentText = [[UITextView alloc] initWithFrame:CGRectMake(ScreenX375(25), ScreenX375(23), SCREEN_WIDTH - ScreenX375(98), ScreenX375(105))];
        commentText.backgroundColor = [UIColor whiteColor];
        commentText.font = Kfont(14);
        commentText.layer.cornerRadius = ScreenX375(4);
        commentText.clipsToBounds = YES;
        commentText.text = @"写评论...";
        commentText.tag = 555;
        commentText.textColor = REXADECIMALCOLOR(@"#999999");
        commentText.delegate = self;
        [commentBg addSubview:commentText];
        
        UIImage *bgImg = [UIImage gradientColorImageFromColors:@[REXADECIMALCOLOR(@"#ff1162"),REXADECIMALCOLOR(@"#fe4e4e")] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(ScreenX375(103), ScreenX375(112))];
        UIButton *commitBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenX375(112), ScreenX375(144), ScreenX375(103), ScreenX375(30))];
        [commitBtn setTitle:@"提交评论" forState:UIControlStateNormal];
        [commitBtn setTitleColor:REXADECIMALCOLOR(@"#ffffff") forState:UIControlStateNormal];
        commitBtn.titleLabel.font = Kfont(15);
        [commitBtn setBackgroundImage:bgImg forState:UIControlStateNormal];
        commitBtn.layer.cornerRadius = ScreenX375(15);
        commitBtn.layer.masksToBounds = YES;
        [commitBtn addTarget:self action:@selector(commitComment) forControlEvents:UIControlEventTouchUpInside];
        [commentBg addSubview:commitBtn];
    }
    [kKeyWindow addSubview:_commentView];
}

- (void)commitComment {
    UITextView *textView = [_commentView viewWithTag:555];
    if (textView.text.length == 0) {
        [HttpManager showNoteMsg:@"请输入评论内容"];
        return;
    }
    [HttpManager postHttpRequestByPost:[NSString stringWithFormat:@"%@api/appraisePro",REQUESTHEADER] andParameter:@{@"proId":_appraiseId,@"content":textView.text,@"userId":kNSUDefaultReadKey(@"userId")} success:^(id successResponse) {
        NSString *status = [NSString stringWithFormat:@"%@",successResponse[@"code"]];
        if ([status isEqualToString:@"200"]) {
            [HttpManager showNoteMsg:@"评论成功"];
            [self close];
            [self.tableView.mj_header beginRefreshing];
            self.appraiseId = @"";
            textView.textColor = REXADECIMALCOLOR(@"#999999");
            textView.text = @"写评论...";
        }
    } andFailure:^(id failureResponse) {
        [HttpManager showFail];
    }];
}

-(void)textViewDidBeginEditing:(UITextView *)textView{
    if (textView.tag == 555) {
        if ([textView.text isEqualToString:@"写评论..."]) {
            textView.textColor = [UIColor blackColor];
            textView.text = @"";
        }
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.tag == 500) {
        if (textView.text.length < 1) {
            textView.textColor = REXADECIMALCOLOR(@"#999999");
            textView.text = @"写评论...";
        }
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if ([text isEqualToString:@"\n"]) {
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
